﻿using MobileControlGuru.Src;
using MobileControlGuru.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MobileControlGuru.AutoTask
{
    public partial class TaskShowItem : UserControl
    {
         
        public TaskShowItem()
        {
            InitializeComponent();
        }
        public TaskJson.TaskInfo taskInfo;
        public TaskList taskList;
   

        public TaskShowItem(TaskJson.TaskInfo taskInfo)
        {
            this.taskInfo = taskInfo;
            InitializeComponent();
        }
        
        private void TaskShowItem_Load(object sender, EventArgs e)
        {
            if (taskInfo != null)
            {
                this.label1.Text = taskInfo.Name;
                this.inputNumber1.Value = taskInfo.RunTimes;

                this.RefreshState();
            }


        }

        public void RefreshState()
        {
            //if (taskInfo != null)
            //{
            //    switch (taskInfo.State)
            //    {

            //        case TaskJson.TaskStateEnum.notrun:
            //            this.lblState.Text = "未开始运行";
            //            break;
            //        case TaskJson.TaskStateEnum.run:
            //            this.lblState.Text = "运行中";
            //            break;
            //        case TaskJson.TaskStateEnum.done:
            //            this.lblState.Text = "已完成";
            //            break;
            //        default:
            //            break;
            //    }
            //}
        }
        

        private void button2_Click(object sender, EventArgs e)
        {
            var taskinfo = (TaskJson.TaskInfo)this.Tag;
            DialogResult AF = MessageBox.Show("确定删除" + taskinfo.Name + "任务吗？", "确认框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (AF == DialogResult.OK)
            {
                TaskManager.Instance.JobDelete(taskinfo);
                taskList.DelTask(taskinfo.id);
            }
            else
            {

            }

        }



        public void taskStart()
        {
            UpdateButton(btnTaskRun, false);
            UpdateButton(btnDeleteTask, false);
            UpdateButton(btnEditTask, false);

            UpdateButton(btnLog, true);
            UpdateButton(btnStopTask, true);
        }

        public void taskFinished()
        {
            UpdateButton(btnTaskRun, true);
            UpdateButton(btnDeleteTask, true);
            UpdateButton(btnEditTask, true);

            UpdateButton(btnLog, false);
            UpdateButton(btnStopTask, false);

           
        }

        private void UpdateButton(AntdUI.Button button, bool visible)
        {
            if (button.InvokeRequired)
            {
                button.Invoke(new Action(() =>
                {
                    button.Visible = visible;

                }));
            }
            else
            {
                button.Visible = visible;
            }
        }

        private void inputNumber1_ValueChanged(object sender, decimal value)
        {
            taskInfo.RunTimes = Convert.ToInt32(value);
            TaskJson.EditTask(taskInfo);
        }



        private void button5_Click(object sender, EventArgs e)
        {
            //runWindow.taskRun.cts.Cancel();
           
        }

        #region 运行
        private async void buttonRunTask_Click(object sender, EventArgs e)
        {
            AntdUI.Button button = (AntdUI.Button)sender;
            this.btnTaskRun.Enabled = false;
            TaskManager tm = TaskManager.Instance;
            var state = await tm.GetTriggerStateBy(taskInfo.Name);
            if (state == Quartz.TriggerState.None)
            {
                this.btnTaskRun.Enabled = false;
                tm.JobStart(taskInfo);
            }
               
            

          
            Console.WriteLine(state);
        }
        #endregion

        private void btn_Log_Click(object sender, EventArgs e)
        {
            // 确保在UI线程上执行
            if (Application.OpenForms.Count > 0)
            {
                Application.OpenForms[0].Invoke((MethodInvoker)delegate
                {
                    LogForm logForm = LogForm.GetInstance();
                    if (!logForm.Visible)
                    {
                        logForm.Show();
                    }

                });
            }
            else
            {
                // 如果没有打开的窗体，可以考虑其他处理方式
                Console.WriteLine("No open forms to attach the log window to.");
            }
        }

        private void buttonTaskEnd_Click(object sender, EventArgs e)
        {
            TaskManager.Instance.JobEnd(taskInfo);
            this.btnTaskRun.Enabled = true;
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            var taskinfo = (TaskJson.TaskInfo)this.Tag;
            TaskJson.TaskInfo temptaskinfo = new TaskJson.TaskInfo();
            temptaskinfo.Corn = taskinfo.Corn;
            temptaskinfo.DeviceName = taskinfo.DeviceName;
            temptaskinfo.id = taskinfo.id;
            temptaskinfo.Name = taskinfo.Name + "copy";
            temptaskinfo.TaskItems = taskinfo.TaskItems;
            
            TaskJson.AddTask(temptaskinfo);
            TaskList form = (TaskList)Application.OpenForms["TaskList"];
            form.RefreshTaskList();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            var taskinfo = (TaskJson.TaskInfo)this.Tag;

            taskList.ShowEdit(taskinfo);
        }
    }
}
